package com.example.demo.service.impl;

import com.example.demo.config.LoginInterceptor;
import com.example.demo.domain.dto.UserLoginDto;
import com.example.demo.enums.UserPermission;
import com.example.demo.service.interfaces.LoginService;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    private static final String LOGIN_KEY = "login_key";
    private static Map<String, UserLoginDto> userMap = new HashMap<>();

    @Override
    public void addUser(HttpServletRequest request, HttpServletResponse response, String id, List<UserPermission> permissions) {
        // todo permission 查询用户信息及权限
        UserLoginDto userLoginDto = new UserLoginDto();
        userLoginDto.setId(id);
        userLoginDto.setPermissions(permissions);

        HttpSession session = request.getSession();
        session.setAttribute(LoginInterceptor.USER_KEY, userLoginDto);
        List<Cookie> list = new ArrayList<>();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (LOGIN_KEY.equals(cookie.getName())) { // 已有用户，
                userMap.remove(cookie.getValue()); // 删除已有
                list.add(cookie);
            }
        }
        if (list.isEmpty()) {
            // new user
            Cookie cookie = new Cookie(LOGIN_KEY, session.getId());
            cookie.setPath("/");
            response.addCookie(cookie);
        } else {
            for (Cookie cookie : list) {
                cookie.setValue(session.getId()); // 更新sessionId
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }

        userMap.put(session.getId(), userLoginDto);
    }

    @Override
    public UserLoginDto getUser(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (LOGIN_KEY.equals(cookie.getName()) && cookie.getValue() != null) {
                    return userMap.get(cookie.getValue());
                }
            }
        }
        return null;
    }


    @Override
    public void removeUser(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        HttpSession session = request.getSession();
        session.removeAttribute(LoginInterceptor.USER_KEY);
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String value = cookie.getValue();
                if (LOGIN_KEY.equals(cookie.getName()) && value != null) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    userMap.remove(value);
                }
            }
        }
    }

    @Override
    public Map<String, UserLoginDto> getUserMap() {
        return userMap;
    }

    @Override
    public void setUserMap(Map<String, UserLoginDto> map) {
        userMap = map;
    }
}
